<?php

/**
 * DYSHOP
 * ============================================================================
 * 版权所有 2005-2028 杭州鼎易信息科技有限公司，并保留所有权利。
 * 网站地址: https://www.doing.net.cn
 * ==========================================
 * 本系统由鼎易科技团队开发 156791994@qq.com
 * ============================================================================
 * $Author: LAOWU 2020-06-06 $
 *
 */
namespace app\index\controller;
use think\facade\View;
use think\facade\Db;
use think\facade\Request;
use app\BaseController;
use think\facade\Config;
use think\facade\Cache;
use think\facade\Cookie;

use app\common\model\Category as CategoryModel;

class Base  extends BaseController
{
    public $user_id;
    /**
     * 析构函数
     */
    function __construct(){

        Request::isAjax() ? define('IS_AJAX',true) : define('IS_AJAX',false);
        (Request::method() == 'GET') ? define('IS_GET',true) : define('IS_GET',false);
        (Request::method() == 'POST') ? define('IS_POST',true) : define('IS_POST',false);

        define('CONTROLLER_NAME',Request::controller(true)); // 当前控制器
        define('ACTION_NAME',Request::action()); // 当前操作名称是

        //config设置
        $webconfig = DB::name("webconfig")->select();
        $webconfig_arr = [];
        foreach($webconfig as $v){
            $webconfig_arr[$v['varname']] = $v['varvalue'];
        }
        Config::set($webconfig_arr, 'webconfig');
        View::assign('webconfig',$webconfig_arr);

        //栏目 start
        $catid = input('param.catid','');
        $id = input('param.id','');
        $page = input('param.page',1);

        //栏目遍历
        $CategoryModel = new CategoryModel();
        $cate_one = $CategoryModel
            ->where('parentid','=',0)
            ->where('is_open','=',1)
            ->order('orderid asc')->select();
        View::assign('cate_one',$cate_one);

        $pid = getPid($catid);
        $father_one = $CategoryModel->where('id','=',$pid)->find();

//        View::assign('module',$father_one['module']);
        View::assign('father_one',$father_one);
        View::assign('catid',$catid);
        View::assign('id',$id);
        View::assign('page',$page);
        View::assign('pid',$pid);
        //栏目 end

        $this->checkLogin(); // 检查是否登录

        $this->user_id = Cookie::get('user_id');

        // 控制器初始化
        $this->initialize();
    }

    public function checkLogin(){
        // 判断哪些控制器的 哪些方法需要登录验证的 //暂时放置
        $check_arr = array(
            'users' => array('user_center','user_center_edit','user_center_order','user_center_order_details','user_center_system','joblist','user_center_safe','user_center_update_mobile','user_center_update_pwd'
            ),
            'order'=>array('order_confirm'),
        );
        //转换为小写
        $controller_name = strtolower(CONTROLLER_NAME);
        $action_name = strtolower(ACTION_NAME);
        if(in_array($controller_name,array_keys($check_arr)) && in_array($action_name, $check_arr[$controller_name])){
            if(!Cookie::get('user_id')){
                $this->redirect(url('index/index'));
            }
        }
    }


}